<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>git操作指南（1） | 小小错题本</title>
    <meta name="description" content="PITrjA.jpg

小公司一般都会使用 svn 托管代码。工作原因，需要使用 git。没办法，硬着头皮上。第一次在超大的代码库中用 git 提交进去自己的两行字符串，还是挺兴奋的呢。【滑稽.jpg】

安装配置

安装：

  这个好像没什么说的，百度 git--&gt;傻瓜下一步安装【手动狗头.j ...">
    <meta name="generator" content="VuePress 1.3.0">
    
    
    <link rel="preload" href="/wrong_question_static_page/assets/css/0.styles.f30cb765.css" as="style"><link rel="preload" href="/wrong_question_static_page/assets/js/app.9022c753.js" as="script"><link rel="preload" href="/wrong_question_static_page/assets/js/4.8c1e4aa5.js" as="script"><link rel="preload" href="/wrong_question_static_page/assets/js/5.33f70f09.js" as="script"><link rel="preload" href="/wrong_question_static_page/assets/js/13.9d026eb1.js" as="script"><link rel="prefetch" href="/wrong_question_static_page/assets/js/1.ec18c3a1.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/10.697d33d5.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/11.488795fa.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/12.12a9963e.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/14.c44f0e4a.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/15.a258b8bb.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/16.92328039.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/17.c4f16e4e.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/18.4dfa396f.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/19.edba2346.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/20.082608a3.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/21.5ae0ad82.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/22.89457782.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/23.53bf9e28.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/24.ae06dfc6.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/25.1d7d04a4.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/6.af07a62f.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/7.2c342931.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/8.2545b8d2.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/9.0eaa1c27.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/vuejs-paginate.c4011005.js">
    <link rel="stylesheet" href="/wrong_question_static_page/assets/css/0.styles.f30cb765.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div id="vuepress-theme-blog__global-layout"><section id="header-wrapper"><header id="header"><div class="header-wrapper"><div class="title"><a href="/wrong_question_static_page/" class="nav-link home-link">小小错题本 </a></div> <div class="header-right-wrap"><ul class="nav"><li class="nav-item"><a href="/wrong_question_static_page/tag/" class="nav-link">分类</a></li></ul> <div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <!----></div></div></header></section> <div id="mobile-header"><div class="mobile-header-bar"><div class="mobile-header-title"><a href="/wrong_question_static_page/" class="nav-link mobile-home-link">小小错题本 </a> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-menu"><line x1="3" y1="12" x2="21" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line></svg></div> <div class="mobile-menu-wrapper"><hr class="menu-divider"> <ul class="mobile-nav"><li class="mobile-nav-item"><a href="/wrong_question_static_page/tag/" class="nav-link">分类</a></li> <li class="mobile-nav-item"><!----></li></ul></div></div></div> <div class="content-wrapper"><div id="vuepress-theme-blog__post-layout"><article itemscope="itemscope" itemtype="https://schema.org/BlogPosting" class="vuepress-blog-theme-content"><header><h1 itemprop="name headline" class="post-title">
        git操作指南（1）
      </h1> <div class="post-meta"><!----> <div class="post-meta-date"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-clock"><circle cx="12" cy="12" r="10"></circle><polyline points="12 6 12 12 16 14"></polyline></svg> <time pubdate itemprop="datePublished" datetime="2018-08-20T09:00:38.000Z">
      Mon Aug 20 2018
    </time></div> <ul itemprop="keywords" class="post-meta-tags"><li class="post-tag" data-v-2ac33c20><a href="/wrong_question_static_page/tag/git" data-v-2ac33c20> git </a></li><li class="post-tag" data-v-2ac33c20><a href="/wrong_question_static_page/tag/前端" data-v-2ac33c20> 前端 </a></li></ul></div></header> <div itemprop="articleBody" class="content__default"><p><img src="https://s1.ax1x.com/2018/08/21/PITrjA.jpg" alt="PITrjA.jpg"></p> <p>小公司一般都会使用 svn 托管代码。工作原因，需要使用 git。没办法，硬着头皮上。第一次在超大的代码库中用 git 提交进去自己的两行字符串，还是挺兴奋的呢。【滑稽.jpg】</p> <h2 id="安装配置"><a href="#安装配置" class="header-anchor">#</a> 安装配置</h2> <ul><li><p>安装：</p> <p>这个好像没什么说的，百度 git--&gt;傻瓜下一步安装【手动狗头.jpg】</p></li> <li><p>配置：</p> <div class="language-bash extra-class"><pre class="language-bash"><code>$ <span class="token function">git</span> config --global user.name <span class="token string">&quot;John Doe&quot;</span>
$ <span class="token function">git</span> config --global user.email johndoe@example.com
</code></pre></div></li> <li><p>关联 github</p> <ol><li>生成密钥对</li></ol> <ul><li>大多数 Git 服务器都会选择使用 SSH 公钥来进行授权。系统中的每个用户都必须提供一个公钥用于授权，没有的话就要生成一个。生成公钥的过程在所有操作系统上都差不多。首先你要确认一下本机是否已经有一个公钥。SSH 公钥默认储存在账户的主目录下的 ~/.ssh 目录。进去看看：</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code>$ <span class="token builtin class-name">cd</span> ~/.ssh
$ <span class="token function">ls</span>
authorized_keys2  id_dsa  known_hosts config  id_dsa.pub
</code></pre></div><ul><li>假如没有这些文件，甚至连 .ssh 目录都没有，可以用 ssh-keygen 来创建。该程序在 Linux/Mac 系统上由 SSH 包提供，而在 Windows 上则包含在 MSysGit 包里：</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code>$ ssh-keygen -t rsa -C <span class="token string">&quot;your_email@youremail.com&quot;</span>

Creates a new <span class="token function">ssh</span> key using the provided email <span class="token comment"># Generating public/private rsa key pair.</span>

Enter <span class="token function">file</span> <span class="token keyword">in</span> <span class="token function">which</span> to save the key <span class="token punctuation">(</span>/home/you/.ssh/id_rsa<span class="token punctuation">)</span>:
</code></pre></div><p>直接按 Enter 就行。然后，会提示你输入密码，如下(建议输一个，安全一点，当然不输也行，应该不会有人闲的无聊冒充你去修改你的代码)：</p> <div class="language-bash extra-class"><pre class="language-bash"><code>Enter same passphrase again: <span class="token punctuation">[</span>Type passphrase again<span class="token punctuation">]</span>
</code></pre></div><p>完了之后，大概是这样：</p> <div class="language-bash extra-class"><pre class="language-bash"><code>Your public key has been saved <span class="token keyword">in</span> /home/you/.ssh/id_rsa.pub.
The key fingerprint is: <span class="token comment"># 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@youremail.com</span>
</code></pre></div><p>2.添加公钥到你的远程仓库（github）</p> <p>2.1、查看你生成的公钥：</p> <div class="language-bash extra-class"><pre class="language-bash"><code>$ <span class="token function">cat</span> ~/.ssh/id_rsa.pub

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0X6L1zLL4VHuvGb8aJH3ippTozmReSUzgntvk434aJ/v7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8vR3c8E7CjZN733f5AL8uEYJA+YZevY5UCvEg+umT7PHghKYaJwaCxV7sjYP7Z6V79OMCEAGDNXC26IBMdMgOluQjp6o6j2KAdtRBdCDS/QIU5THQDxJ9lBXjk1fiq9tITo/aXBvjZeD+gH/Apkh/0GbO8VQLiYYmNfqqAHHeXdltORn8N7C9lOa/UW3KM7QdXo6J0GFlBVQeTE/IGqhMS5PMln3 admin@admin-PC
</code></pre></div><p>2.2、登陆你的github帐户。点击你的头像，然后 Settings -&gt; 左栏点击 SSH and GPG keys -&gt; 点击 New SSH key</p> <p>2.3、然后你复制上面的公钥内容，粘贴进“Key”文本域内。 title域，自己随便起个名字。</p> <p>2.4、点击 Add key。</p> <p>完成以后，验证下这个key是不是正常工作：</p> <div class="language-bash extra-class"><pre class="language-bash"><code>$ <span class="token function">ssh</span> -T git@github.com

Attempts to <span class="token function">ssh</span> to github

Hi xxx<span class="token operator">!</span> You've successfully authenticated, but GitHub does not <span class="token comment"># provide shell access.</span>
</code></pre></div></li></ul> <h2 id="git-常用命令"><a href="#git-常用命令" class="header-anchor">#</a> git 常用命令</h2> <ul><li>拉取代码</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">git</span> clone git@githome****    <span class="token comment">#从远程仓库克隆代码【master分支】</span>

<span class="token function">git</span> clone -b xxx git@githome****    <span class="token comment">#从远程仓库克隆代码【xxx分支】</span>
</code></pre></div><ul><li><p>修改代码</p> <p>进入代码文件夹后开发代码</p></li> <li><p>检查工作区状态</p></li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">git</span> status
</code></pre></div><ul><li>检查分支</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">git</span> branch  <span class="token comment">#查看目前所在分支</span>
<span class="token function">git</span> checkout dev  <span class="token comment">#切换到dev分支</span>
</code></pre></div><ul><li>增加改动到缓存区</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">git</span> <span class="token function">add</span> *   <span class="token comment">#增加所有改动到缓存区</span>
</code></pre></div><ul><li>提交代码到本地仓库</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">git</span> commit -m <span class="token string">&quot;提交描述信息&quot;</span>
</code></pre></div><ul><li>推送到远程服务器</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">git</span>  push    <span class="token comment">#推送代码到master分支</span>
<span class="token function">git</span>  push origin -u xxx  <span class="token comment">#推送代码到xxx分支</span>
</code></pre></div></div> <footer><!----> <hr> <!----></footer></article> <div class="sticker vuepress-toc"><div class="vuepress-toc-item vuepress-toc-h2 active"><a href="#安装配置" title="安装配置">安装配置</a></div><div class="vuepress-toc-item vuepress-toc-h2"><a href="#git-常用命令" title="git 常用命令">git 常用命令</a></div></div></div></div> <footer class="footer" data-v-06e1b74b><div class="footer-left-wrap" data-v-06e1b74b><ul class="contact" data-v-06e1b74b></ul></div> <div class="footer-right-wrap" data-v-06e1b74b><ul class="copyright" data-v-06e1b74b></ul></div></footer></div><div class="global-ui"><!----></div></div>
    <script src="/wrong_question_static_page/assets/js/app.9022c753.js" defer></script><script src="/wrong_question_static_page/assets/js/4.8c1e4aa5.js" defer></script><script src="/wrong_question_static_page/assets/js/5.33f70f09.js" defer></script><script src="/wrong_question_static_page/assets/js/13.9d026eb1.js" defer></script>
  </body>
</html>
